package cn.com.dhcc.ddi.task.service.notify;

import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.tag.CommonStaticCode;
import cn.com.dhcc.app.pub.core.tag.staticcode.StaticCode;
import cn.com.dhcc.app.pub.core.util.settlement.compute.SettUtil;
import cn.com.dhcc.ddi.task.dao.TableLogDao;
import cn.com.dhcc.ddi.task.dao.TaskDao;
import cn.com.dhcc.ddi.task.dao.TaskExecuteLogDao;
import cn.com.dhcc.ddi.task.vo.TableLog;
import cn.com.dhcc.ddi.task.vo.Task;
import cn.com.dhcc.ddi.task.vo.TaskExecuteLog;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 数据库数据源抽象类
 * @日期：2016/11/30 15:06
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Service
public abstract class NotifyDBAbstract implements NotifyInterface {
    private static Logger logger = Log.getLogger(Log.LOGTYPE.DDI);
    @Autowired
    private TaskExecuteLogDao taskExecuteLogDao;
    @Autowired
    private TaskDao taskDao;
    @Autowired
    private TableLogDao tableLogDao;

    /**
     * 数据库数据源通知
     * @param taskLogId
     * @param status
     * @param msg
     * @throws DaoException
     */
    public void notify(String taskLogId, String status, String msg) throws DaoException {
        TaskExecuteLog taskExecuteLog = taskExecuteLogDao.get(taskLogId);
        TaskExecuteLog extractLog = taskExecuteLogDao.getExtractLog(taskLogId);
        Task task =  taskDao.get(taskExecuteLog.getTaskId());
        boolean exchangeSuccess = false;
        if(CommonStaticCode.SUCCESSFAIL.SUCCESS.getStatus().equals(status)){
            exchangeSuccess = true;
        }
        if(exchangeSuccess){
            TaskExecuteLog taskLinkLog = taskExecuteLogDao.getLinkLog(taskLogId, StaticCode.LINK_TYPE.EXTRACT.getCode());
            if(task.getTaskFreqType().equalsIgnoreCase(StaticCode.TASK_FREQ_TYPE.ONETIME.getCode())){
                task.setStatus(StaticCode.TASK_STATUS.OVER.getCode());
            }else{
                task.setStatus(StaticCode.TASK_STATUS.WAIT.getCode());
            }
            taskExecuteLog.setLogStatus(CommonStaticCode.SUCCESSFAIL.SUCCESS.getStatus());
            taskExecuteLog.setLogMsg("交换成功");
            //交换数据量
            taskExecuteLog.setTotal(taskLinkLog.getTotal());
        }else{
            logger.info("交换失败，下次重发。");
            taskExecuteLog.setLogMsg("交换成功");
            taskExecuteLog.setLogStatus(CommonStaticCode.SUCCESSFAIL.FAIL.getStatus());
            task.setStatus(StaticCode.TASK_STATUS.ERROR.getCode());
        }
        if(CommonStaticCode.YESNO.YES.getStatus().equals(task.getIsSett())) {
            if (exchangeSuccess) {
                SettUtil.dataMerge(task.getId(), taskLogId, taskExecuteLog.getLinkStartTime());
            } else {
                SettUtil.dataDelete(taskLogId);
            }
        }
        List<TableLog> tableLogs = tableLogDao.listByTaskId(task.getId());
        for (TableLog tableLog:tableLogs) {
            if(exchangeSuccess){
                String tempLastValue = tableLog.getTempLastValue();
                //设置上次同步增量值
                if(tempLastValue!=null && !"".equals(tempLastValue)){
                    tableLog.setLastValue(tempLastValue);
                }
            }
            tableLog.setTempLastValue("");
            tableLogDao.update(tableLog);
        }
        taskDao.update(task);
        taskExecuteLog.setTotal(extractLog.getTotal());
        taskExecuteLog.setLinkEndTime(new Date());
        taskExecuteLogDao.update(taskExecuteLog);
    }
}
